//
//  WBNetworkManager+Extension.swift
//  Weibo
//
//  Created by wade on 17/2/22.
//  Copyright © 2017年 wade. All rights reserved.
//

import Foundation

extension WBNetworkManager
{
    func loadAccessToken(code:String,completion:@escaping (Bool)->()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
     
        let params = ["client_id":AppKey,
                      "client_secret":AppSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":AppUri]
        
        
        requestToken(method: .POST, URLString: urlString, parameters: params) { (json, bool) in
            print(json ?? "")
            
            self.userAccount.yy_modelSet(with: (json as? [String : Any]) ?? [:])
            
            print(self.userAccount)
            
            WBNetworkManager.shared.loadUserInfo(completion: { dict in
                
                print("用户信息：\(dict)")
                
                self.userAccount.yy_modelSet(with: dict)
                
                self.userAccount.saveAccount()
                
                completion(bool)
            })
            
            
            
        }
        
    }
    
    
    
    func statusList(since_id:Int64,max_id:Int64,completion:@escaping ([[String:Any]]?,Bool)->()) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = ["since_id":"\(since_id)","max_id":"\(max_id > 0 ? max_id - 1 : 0)"]
        
        request(URLString: urlString, parameters: params, completion: { (json, bool) in
            
        
            guard let vvv = json as? [String:Any],
                let value = vvv["statuses"] as? [[String:Any]] else
            {
                completion(nil,false)
                
                return
            }
            
            completion(value,bool)
            
        })
        
    }
    
    
     //1096774112
    
    func unreadCount(completion:@escaping (Int)->()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let params = ["uid":uid]
        
        request(URLString: urlString, parameters: params) { (json, bool) in
            
            let dict = json as? [String:Any]
            
            let count = dict?["status"] as? Int
            
            completion(count ?? 0)
        }
        
        
    }
    
    func loadUserInfo(completion:@escaping ([String:Any])->()) {
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let params = ["uid":uid]
        
        request(URLString: urlString, parameters: params) { (json, bool) in
            
            
            completion((json as? [String:Any]) ?? [:])
        }
    }
    
    
    
}



